#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/22 0022 21:05
# @Author  : GXl
# @File    : views.py
# @Software: win10 Tensorflow1.13.1 python3.7

import json
from flask import request,g
from . import myauth
from utils.apiutils import *
from app import db
from ..models import User
import random



logger = logutils.getlogger(__file__)


@myauth.route("/test", methods=['GET', 'POST','DELETE','PUT'])
def test():
    return jsonData("test ok")




@myauth.route('/login', methods=['GET', 'POST'])
def login():
    reqjson=json.loads(request.data)
    print(reqjson)
    user = User.query.filter_by(username = reqjson['username']).filter_by(password = reqjson['password']).first()
    if not user:
        return jsonerror('未找到用户，username:{}'.format(reqjson['username']),status=401)
    user.generate_auth_token()
    return jsonData(user)


@myauth.route('/logout', methods=['GET', 'POST'])
def logout():
    return jsonMsg('已退出')

@myauth.route('/step2code', methods=['GET', 'POST'])
def step2code():
    logger.debug('******************step2code**************************')
    step2code_data=request.data
    logger.debug("data={}".format(step2code_data))
    num=random.randint(0,10)%2
    return jsonData(num)